/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.skycode.segu.mbean;

import com.skycode.core.common.UtilCore;
import com.skycode.core.mbean.GenericMBean;
import com.skycode.core.mbean.SessionMBean;
import com.skycode.segu.dto.UsuarioDto;
import com.skycode.segu.manager.UsuarioManager;
import java.io.Serializable;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Jeferson
 */
@Controller("loginMBean")
@Scope("session")
public class LoginMBean extends GenericMBean implements Serializable {

    private static Logger logger = Logger.getLogger(LoginMBean.class);
    private String usuario;
    private String password;
    private final String urlLogin = "/login.xhtml";
    @Autowired
    private SessionMBean sessionMBean;
    @Resource
    private UsuarioManager usuarioManager;

    public String iniciar() {
        this.usuario = "";
        this.password = "";

        return this.urlLogin;
    }

    public String ingresar() {
        if (validarUsuario()) {
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            this.sessionMBean.setSessionTerminal(request.getRemoteAddr());
            sessionMBean.setMensajeExpiraSession("");
            request.getSession().setAttribute("terminal", request.getRemoteAddr());

            return "/inicio.xhtml";
        } else {
            return null;
        }
    }
    
    private boolean validarUsuario() {
        this.sessionMBean.setSessionUsuarioDto(null);
        UsuarioDto usuarioDto = usuarioManager.login(this.usuario, this.password);
        if (usuarioDto != null) {
            this.sessionMBean.setSessionUsuarioDto(usuarioDto);
            String msj = UtilCore.Internacionalizacion.getMensajeInternacional("frm.login.ok");
            showMessage(msj);
            return true;
        } else {
            String msj = UtilCore.Internacionalizacion.getMensajeInternacional("frm.login.no");
            showError(msj);
            return false;
        }
    }
    
    public String cerrar() {
        this.sessionMBean.setSessionUsuarioDto(null);
        this.sessionMBean.setSessionTerminal(null);
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        request.getSession().removeAttribute("terminal");

        request.getSession().invalidate();
        sessionMBean.setMensajeExpiraSession("");
        return "/inicio.xhtml";
    }

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        LoginMBean.logger = logger;
    }

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrlLogin() {
        return urlLogin;
    }

    public SessionMBean getSessionMBean() {
        return sessionMBean;
    }

    public void setSessionMBean(SessionMBean sessionMBean) {
        this.sessionMBean = sessionMBean;
    }
}
